home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d962.lha / EnvTool / EnvTool.c < prev    next >
C/C++ Source or Header  |  1994-01-05  |  6KB  |  266 lines

  1. /****************************************************************************
  2.  *    EnvTool 0.1
  3.  *
  4.  *    Alpha Release, 27 December 1993
  5.  *    Compiled with DICE
  6.  *
  7.  *    Placed in the public domain, by Dan Fish
  8.  *    No rights reserved.
  9.  *
  10.  *    Please freely-distribute this program! Enhance it, send it back to 
  11.  *    us for use with the AmigaLib library.  (After all... it's to make
  12.  *    the library better for you!)
  13.  *
  14.  *    Amiga Library Services
  15.  *    610 N. Alma School Rd.
  16.  *    Suite 18
  17.  *    Chandler, Az.  85224-3687
  18.  *
  19.  *   or Email to:
  20.  *
  21.  *      fnf@fishpond.cygnus.com
  22.  *
  23.  *
  24.  ****************************************************************************/
  25. #include <exec/types.h>
  26. #include <libraries/dos.h>
  27. #include <workbench/workbench.h>
  28. #include <workbench/startup.h>
  29.  
  30. #include <clib/exec_protos.h>
  31. #include <clib/dos_protos.h>
  32. #include <clib/intuition_protos.h>
  33. #include <clib/icon_protos.h>
  34.  
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdarg.h>
  39.  
  40. /* 
  41.  *  Function prototypes
  42.  */
  43.  
  44. void  cleanexit( LONG );
  45. void  HandleArg( char * );
  46. long  Req( UBYTE *, UBYTE *, ... );
  47. char *TTString( struct DiskObject *, char *, char * );
  48.  
  49. #define  EXEVERSION          "v0.1"
  50. #define  EXENAME             "EnvTool"
  51.  
  52. /*
  53.  * DCBack required data. DCBack is a tiny link library
  54.  * written by Jan van den Baard to make it possible to write
  55.  * auto-detachable programs with DICE. 
  56.  */
  57.  
  58. UBYTE                       *_procname      = EXENAME "_" EXEVERSION;
  59. UBYTE                       *_template      = NULL;
  60. UBYTE                       *_exthelp       = NULL;
  61. LONG                         _stack         = 2048L;
  62. LONG                         _priority      = NULL;
  63. LONG                         _BackGroundIO  = NULL;
  64.  
  65. /*
  66.  * The following libraries are all auto-init.
  67.  */
  68. extern struct IntuitionBase        *IntuitionBase;
  69. extern struct UtilityBase          *UtilityBase;
  70. extern struct GadToolsBase         *GadToolsBase;
  71.  
  72. /*
  73.  * Required libraries that are not in DICE's
  74.  * auto-init library.
  75.  */
  76.  
  77. struct Library              *IconBase      = NULL;
  78. struct Library              *WorkbenchBase = NULL;
  79.  
  80. /* The Default command to run if none specified via ToolTypes */
  81.  
  82. UBYTE   *DEFAULT_CMD =   "Run SYS:Utilities/More ";
  83.  
  84. /* Used for debugging    */
  85. UBYTE *conwinname   = "CON:10/10/620/180/libreader.new";
  86.  
  87.  
  88. /*
  89.  * Shell version string.
  90.  */
  91. static UBYTE EnvToolVer[]        =       "$VER: EnvTool 0.1 (27-Dec-93)";
  92.  
  93. /* Opens and allocations we must clean up */
  94.  
  95. FILE *conwin = NULL;  /* simple debugging    */
  96. LONG olddir = -1;
  97. struct WBStartup *WBenchMsg = NULL;
  98.  
  99. int main( int argc, char **argv )
  100. {
  101.   fprintf(stderr, "%s %s must be started from the WorkBench\n"
  102.         EXENAME, EXEVERSION);
  103.   exit(-1);
  104. }
  105.  
  106. void wbmain( struct WBStartup *wbs )
  107.  {
  108.     struct WBArg *wbarg;
  109.     SHORT i;
  110.     int error;
  111.     WBenchMsg = wbs;
  112.  
  113.     if ( ! ( IconBase = OpenLibrary( "icon.library", 34L ))) {
  114.         error = 21L;
  115.         cleanexit( error );
  116.     }
  117.  
  118.     if ( ! ( WorkbenchBase = OpenLibrary( "workbench.library", 37L ))) {
  119.         error = 22L;
  120.         cleanexit( error );
  121.     }
  122.  
  123.     if (WBenchMsg ) {
  124.        /*
  125.         * When WBenchMsg is non-null it means that
  126.         * Libreader was started from the workbench.
  127.         */
  128.  
  129.        /* Used for some simple debugging...
  130.        if(!(conwin = fopen(conwinname,"r+")))
  131.              cleanexit(30L);*/
  132.  
  133.  
  134.       /* Note wbarg++ at end of FOR statement steps through wbargs.
  135.        * First arg is our executable (tool).  Any additional args
  136.        * are projects/icons passed to us via either extend select
  137.        * or default tool method.
  138.        */
  139.  
  140.        for(i=0, wbarg=WBenchMsg->sm_ArgList; i < WBenchMsg->sm_NumArgs;
  141.               i++, wbarg++)
  142.         {
  143.  
  144.  
  145.           /* if there's a directory lock for this wbarg, CD there */
  146.           olddir = -1;
  147.  
  148.           if((wbarg->wa_Lock)&&(*wbarg->wa_Name))
  149.               olddir = CurrentDir(wbarg->wa_Lock);
  150.  
  151.           if((i>0)&&(*wbarg->wa_Name))
  152.            {
  153.         HandleArg(wbarg->wa_Name);        
  154.        }
  155.  
  156.           if(olddir != -1)  CurrentDir(olddir); /* CD back where we were */
  157.  
  158.          } /* end of for */
  159.  
  160.        } /* end of if */
  161.  
  162.     cleanexit(0L);
  163.  
  164.   } /* end of main */
  165.  
  166. void HandleArg(char *arg)
  167.  {
  168.   struct DiskObject *diskobj;
  169.   char *env = (char *)NULL;
  170.   char *env_ttype = (char *)NULL;
  171.   char *def_ttype = (char *)NULL;
  172.   char *prefix;
  173.  
  174.   char temp[81];
  175.   char cmdbuff[81]; 
  176.   int ierr;
  177.  
  178.   /* Open up the icon for this project */
  179.  
  180.   if(!(diskobj = GetDiskObject(arg)))
  181.    {
  182.     (void)Req("Uh-Oh!","Cannot access icon for file:\n\"%s\"", arg); 
  183.     return;
  184.    }
  185.  
  186.   /* start with the basic built-in "default" command */
  187.   sprintf(temp,"%s %s",DEFAULT_CMD,arg);
  188.  
  189.   /* if a default tool is specified, update the command buffer */
  190.   def_ttype = TTString(diskobj,"DEFAULT",NULL);
  191.   if(def_ttype)
  192.       sprintf(temp,"%s %s",def_ttype,arg);
  193.  
  194.   /* if an environment variable is specified and set, update the command */
  195.   env_ttype = TTString(diskobj,"ENV", NULL);
  196.   if (env_ttype)
  197.    {
  198.     if(env = getenv(env_ttype))
  199.       sprintf(temp,"%s %s",env,arg);
  200.    }
  201.  
  202.   /* Check for a PREFIX tooltype */
  203.  
  204.   prefix = TTString(diskobj,"PREFIX", NULL);
  205.   if(prefix)
  206.       sprintf(cmdbuff,"%s %s",prefix,temp);
  207.   else
  208.       strcpy(cmdbuff,temp);
  209.  
  210.   /* spawn the command */
  211.   ierr = system(cmdbuff);
  212.  
  213.   if (ierr != 0)
  214.      (void)Req("Oops!","The command:\n\"%s\"\n\nReturned Code = %ld\n",
  215.          cmdbuff,ierr); 
  216.  
  217.    (void)FreeDiskObject(diskobj);
  218.  
  219. }
  220.  
  221. void cleanexit( LONG code )
  222. {
  223.     if (conwin)             fclose(conwin);
  224.     if ( WorkbenchBase )    CloseLibrary( WorkbenchBase );
  225.     if ( IconBase )         CloseLibrary( IconBase );
  226.     exit( code );
  227.  
  228. }
  229.  
  230. /*
  231.  * Put up a simple requester.
  232.  */
  233. long Req( UBYTE *gadgets, UBYTE *body, ... )
  234. {
  235.     static struct EasyStruct req = {
  236.         sizeof( struct EasyStruct ), NULL, NULL, NULL, NULL };
  237.     va_list                  args;
  238.     LONG                     rc;
  239.  
  240.     va_start( args, body );
  241.  
  242.     req.es_Title        = EXENAME;
  243.     req.es_TextFormat   = body;
  244.     req.es_GadgetFormat = gadgets;
  245.  
  246.     rc = EasyRequestArgs( NULL, &req, NULL, args );
  247.  
  248.     va_end( args );
  249.  
  250.     return( rc );
  251. }
  252.  
  253. /* 
  254.  * TTString returns the argument of the specified String type ToolType 
  255.  * (Provided it exists of course!) ... Kinda like ArgString()
  256.  */
  257.  
  258. char *TTString(struct DiskObject *diskobj, char *tt, char *def)
  259. {
  260.   char *string;
  261.   if (diskobj)
  262.     if (string = FindToolType(diskobj->do_ToolTypes, tt))
  263.       return(string);
  264.   return(def);
  265. }
  266.